import time, curses, subprocess, thread

def pcsound():
	return_code = subprocess.call(["afplay", "playerchange.wav"])
def pssound():
	return_code = subprocess.call(["afplay", "pause.m4a"])
def tcsound():
	return_code = subprocess.call(["afplay", "ticktock.wav"])
def gosound():
	return_code = subprocess.call(["afplay", "gameover.wav"])
def unsound():
	return_code = subprocess.call(["afplay", "undo.wav"])

def main(t):

	curses.noecho()
	curses.cbreak()
	curses.curs_set(0)
	stdscr.nodelay(1)
	t=int(t)
	a,b,az,bz,x=t,t,t,t,t
	s=0
	thread.start_new_thread(tcsound, ())
	while a>0 and b>0 and x>0:
		if s==0:
			c=stdscr.getch()
			stdscr.erase()
			stdscr.addstr(0,0,str(b))
			stdscr.addstr(0,24,str(a))
			stdscr.addstr(2,0,'Press q or p to start, a or l to undo and b to pause')
		if c==ord('b'):
			break
		if c==ord('q') or c==ord('a'):
			if c==ord('q'):
				thread.start_new_thread(pcsound, ())
			if c==ord('a'):
				thread.start_new_thread(unsound, ())
			az=a
			x=a
			s=1
			while x>0:
				c=stdscr.getch()
				if c==ord('p'):
					a=x
					break	
				if c==ord('l'):
					break
				if c==ord('b'):
					thread.start_new_thread(pssound, ())
					stdscr.nodelay(0)
					stdscr.addstr(2,0,'Press b again to resume.')
					stdscr.addstr(3,0,'Press t to start again.')
					c=stdscr.getch()
					if c==ord('b'):
						thread.start_new_thread(pssound, ())
						stdscr.nodelay(1)
					if c==ord('t'):
						thread.start_new_thread(pssound, ())
						stdscr.erase()
						stdscr.nodelay(0)
						curses.echo()
						curses.curs_set(1)
						curses.nocbreak()
						stdscr.addstr(0,0,'Set time limit:')
						main(stdscr.getstr())
				time.sleep(0.01)
				x=x-0.01
				stdscr.erase()
				stdscr.addstr(0,24,str(x))
				stdscr.addstr(0,0,str(b))

		if c==ord('p') or c==ord('l'):
			if c==ord('p'):
				thread.start_new_thread(pcsound, ())
			if c==ord('l'):
				thread.start_new_thread(unsound, ())
			x=b
			s=1
			while x>0:
				c=stdscr.getch()	
				if c==ord('q'):
					b=x
					break
				if c==ord('a'):
					break
				if c==ord('b'):
					thread.start_new_thread(pssound, ())
					stdscr.nodelay(0)
					stdscr.addstr(2,0,'Press b again to resume.')
					stdscr.addstr(3,0,'Press t to start again.')
					c=stdscr.getch()
					if c==ord('b'):
						thread.start_new_thread(pssound, ())
						stdscr.nodelay(1)
					if c==ord('t'):
						thread.start_new_thread(pssound, ())
						stdscr.erase()
						stdscr.nodelay(0)
						curses.echo()
						curses.curs_set(1)
						curses.nocbreak()
						stdscr.addstr(0,0,'Set time limit:')
						main(stdscr.getstr())
				time.sleep(0.01)
				x=x-0.01
				stdscr.erase()
				stdscr.addstr(0,24,str(a))
				stdscr.addstr(0,0,str(x))

	thread.start_new_thread(gosound, ())
	curses.flash()
	stdscr.addstr(2,0,'Game over!')
	stdscr.addstr(3,0,'Press t to start again.')
	while 1==1:
		c=stdscr.getch()
		if c==ord('t'):
			break
	stdscr.erase()
	stdscr.nodelay(0)
	curses.echo()
	curses.curs_set(1)
	curses.nocbreak()
	stdscr.addstr(0,0,'Set time limit (in seconds):')
	main(stdscr.getstr())
	

stdscr=curses.initscr()
stdscr.addstr(0,0,'Set time limit (in seconds):')
main(stdscr.getstr())